package jsmc.opendata.nantes.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jsmc.android.utils.DataBaseHelper;
import jsmc.opendata.nantes.modele.Equipement;
import jsmc.opendata.pratikanant.Constants;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class EquipementDB {

	Context mContext = null;
	private SQLiteDatabase database = null;

	private String DB_PATH;

	private String DB_NAME;
	
	/**
	 * Nom de la table ds la base
	 */
	private static final String EQUIPEMENTS_TABLE = "Equipements_publics_deplacement";
	
	public EquipementDB(Context context) {

		this.mContext = context;
		this.DB_NAME = Constants.DB_NAME;
		this.DB_PATH = Constants.DB_PATH;

	}

	public EquipementDB(Context mcontext, List<Class<?>> asList) {
		
	}

	private void OpenForWriting() {

		NeoLog.v(this.getClass().getSimpleName(),"OpenForWriting");

		DataBaseHelper databaseopener = new DataBaseHelper(mContext,DB_PATH,DB_NAME);
		this.database = databaseopener.getWritableDatabase();
	}

	private void OpenForReading() {

		NeoLog.v(this.getClass().getSimpleName(),"OpenForReading");

		DataBaseHelper databaseopener = new DataBaseHelper(mContext,DB_PATH,DB_NAME);
		this.database = databaseopener.getReadableDatabase();
	}

	private void Close() {
		NeoLog.v(this.getClass().getSimpleName(),"Close");
		this.database.close();
	}

	public void ClearHistory(){

		OpenForReading();
		this.database.execSQL("DELETE FROM history");
		this.database.close();

	}
	
	


	public Map<Integer, Equipement> GetEquipement(int theme){

		NeoLog.v(this.getClass().getSimpleName(),"GetEquipement");

		Map<Integer, Equipement>  mapEquipements = new HashMap<Integer, Equipement>();

		OpenForReading();

		Cursor cursor = this.database.query(
				EQUIPEMENTS_TABLE, 
				null, "THEME="+theme, null, null, null,
				"NOM_COMPLET ASC");
				
		//new String[]{"_IDOBJ","NOM_COMPLET","COMMUNE","ADRESSE","CODE_POSTAL","LONG_WGS84","LAT_WGS84"}
		
		while(cursor.moveToNext()) {

			Equipement equipement = new Equipement();
			int _IDOBJ = cursor.getInt(cursor.getColumnIndex("_IDOBJ"));
			
			NeoLog.v(this.getClass().getSimpleName(),"GetEquipement _IDOBJ:"+_IDOBJ);
			/*
			String number = cursor.getString(cursor.getColumnIndex("number"));
			int date = cursor.getInt(cursor.getColumnIndex("date"));
			
			int duration = cursor.getInt(cursor.getColumnIndex("duration"));

			History current = new History(id,number,"",date,duration);
			current.SetContactDesc(contacts);
			 */
			
			mapEquipements.put(_IDOBJ, equipement);

		}

		Close();

		return mapEquipements;

	}

	public void queryDbAndInsert(Class<Equipement> class1,
			EquipementInsertObject equipementInsertObject) {
		// TODO Auto-generated method stub
		
	}

}
